<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/3
 * Time: 9:50
 */
namespace Admin\Controller;

class FamilyMemberController extends AdminController
{
    public function search(){
        $param = $this->getParam();
        $modelName = $this->modelName;
        $scope = array();
        $relation = array('customer');
        $search = $this->getSearch();
        $page = $this->getPage();
        $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
        if($data){
            $res['stat'] = 1;
            $res['data'] = $data;
            $res['count'] = $data = D($modelName)->where($param)->where($search)->scope($scope)->count();
            $res['msg'] = '查找到1条数据。';
        }elseif($data == null){
            $res['stat'] = 1;
            $res['data'] = array();
            $res['count'] = 0;
            $res['msg'] = '查找到0条数据。';
        }else{
            $res['stat'] = 0;
            $res['data'] = $data;
            $res['msg'] = '查找失败！';
        }
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function read(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array('customer','family');
            if($param[$pk]){
                $data = D($modelName)->field($field)->relation($relation)->find($param[$pk]);
            }else{
                $data = D($modelName)->where($param)->field($field)->relation($relation)->find();
            }
            if($data){
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '查找到1条数据。';
            }elseif($data == null){
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找到0条数据。';
            }else{
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function add(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $field = array();
            $relation = array();
            $create = $field ? D($modelName)->field($field)->create($param,1) : D($modelName)->create($param,1);
            if($create){
                $data = D($modelName)->relation($relation)->add();
                if($data){
                    $pk = D($modelName)->getPk();
                    $res['stat'] = 1;
                    if($pk){
                        $res['pk'] = $data;
                    }
                    $res['data'] = $data;
                    $res['msg'] = '新增数据1条。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '新增数据失败！';
                }
            }else{
                $res['stat'] = 0;
                $res['msg'] = D($modelName)->getError();
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function update(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array();
//            $where = array(); //自定义
            $create = $field ? D($modelName)->field($field)->create($param,2) : D($modelName)->create($param,2);
            if($create){
                if($param[$pk]){
                    $data = D($modelName)->relation($relation)->save();
                }else{
                    foreach($param as $k=>$v){
                        if($k == 'customer_id' || $k == 'family_id')
                        $where[$k] = strToWhere($v);
                    }
                    $data = D($modelName)->where($where)->relation($relation)->save(array('prefix'=>$param['prefix']));
                }
                if($data){
                    if($param['prefix'] == '户主'){
                        D($modelName)->where(array('family_id'=>array('eq',$param['family_id']),'customer_id'=>array('neq',$param['customer_id'])))->save(array('prefix'=>'成员'));
                    }
                    $res['stat'] = 1;
                    $res['data'] = $data;
                    $res['msg'] = '更新数据成功。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '更新数据失败！';
                }
            }else{
                $res['stat'] = 0;
                $res['msg'] = D($modelName)->getError();
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function delete(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $relation = array();
            if($param[$pk]){
                $data = D($modelName)->relation($relation)->delete($param[$pk]);
            }else{
                foreach($param as $k=>$v){
                    $where[$k] = strToWhere($v);
                }
                $data = D($modelName)->where($where)->relation($relation)->delete();
            }
            if($data){
                if(!D($modelName)->where(array('family_id'=>$param['family_id'],'prefix'=>'户主'))->find()){
                    D($modelName)->where(array('family_id'=>$param['family_id']))->order(array('create_time'))->limit(1)->save(array('prefix'=>'户主'));
                }
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '删除数据'.$data.'条。';
            }else{
                $res['stat'] = 0;
                $res['msg'] = '删除数据失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

//    public function add()
//    {
//        if(!IS_POST){
//            $res['stat'] = 0;
//            $res['msg'] = '请不要通过恶意攻击！';
//        }else{
//            $model = D($this->model);
//            $param = $this->getParam();
//            if($model->where(array('customer_id'=>$param['customer_id'],'family_id'=>$param['family_id']))->select()){
//                $res['stat'] = 0;
//                $res['msg'] = '该客户已经在此家庭';
//            }else{
//                $result = $model->handle(__FUNCTION__,$where='',$param,$this->field,$this->order,$this->getPage());
//                if($result['stat']){
//                    if(!$model->where(array('family_id'=>$param['family_id'],'prefix'=>'户主'))->select()){
//                        $model->where(array('family_id'=>$param['family_id']))->order('create_time')->limit(1)->save(array('prefix'=>'户主'));
//                    }
//                }
//                $res = $result;
//            }
//            $this->ajaxReturn($res);
//        }
//    }

//    public function update()
//    {
//        if(!IS_POST){
//            $res['stat'] = 0;
//            $res['msg'] = '请不要通过恶意攻击！';
//        }else{
//            $param = $this->getParam();
//            if($param['prefix'] == '户主'){
//                D($this->model)->where(array('family_id'=>$param['family_id']))->save(array('prefix'=>'成员'));
//            }
//            $where['family_id'] = $param['family_id'];
//            $where['customer_id'] = $param['customer_id'];
//            $result = D($this->model)->handle('save',$where,$param,$this->field,$this->order,$this->getPage());
//            $res = $result;
//            $this->ajaxReturn($res);
//        }
//    }
//
//    public function delete()
//    {
//        if(!IS_POST){
//            $res['stat'] = 0;
//            $res['msg'] = '请不要通过恶意攻击！';
//        }else{
//            $where = $this->getParamWhere();
//            $result = D($this->model)->handle(__FUNCTION__,$where,$param='',$this->field,$this->order,$this->getPage());
//            if($result['stat']){
//                if(!D($this->model)->where(array('family_id'=>$where['family_id'],'prefix'=>'户主'))->select()){
//                    D($this->model)->where(array('family_id'=>$where['family_id']))->order('create_time')->limit(1)->save(array('prefix'=>'户主'));
//                }
//            }
//            $res = $result;
//            $this->ajaxReturn($res);
//        }
//    }
}